package com.jasonzhou.onetomanycontrol.stack;

import androidx.annotation.NonNull;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateTimeUtils {
    public static final long MILLIS_PER_DAY = 86400000L;
    private static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static volatile ThreadLocal<SimpleDateFormat> sDateFormatThreadLocal;

    public DateTimeUtils() {
    }

    public static String getCurDateTime() {
        return getCurDateTime("yyyy-MM-dd HH:mm:ss");
    }

    public static String getCurDateTime(String format) {
        SimpleDateFormat dateFormat = getDateFormat();
        dateFormat.applyPattern(format);
        return dateFormat.format(Calendar.getInstance().getTime());
    }

    public static String getDateBySecond(long seconds) {
        return getDateByMillis(seconds * 1000L);
    }

    public static String getDateByMillis(long millisTime) {
        return getDateTime(millisTime, "yyyy-MM-dd HH:mm:ss");
    }

    public static String getDateBySecond(long seconds, String format) {
        return getDateByMillis(seconds * 1000L, format);
    }

    public static String getDateByMillis(long millisTime, String format) {
        return getDateTime(millisTime, format);
    }

    public static String getDateTime(long millisTime, String format) {
        SimpleDateFormat dateFormat = getDateFormat();
        dateFormat.applyPattern(format);
        return dateFormat.format(new Date(millisTime));
    }

    public static String getDateTime(long millisTime, String format, TimeZone timeZone) {
        SimpleDateFormat dateFormat = getDateFormat();
        TimeZone originTimeZone = dateFormat.getTimeZone();
        dateFormat.setTimeZone(timeZone);
        dateFormat.applyPattern(format);
        String result = dateFormat.format(new Date(millisTime));
        dateFormat.setTimeZone(originTimeZone);
        return result;
    }

    public static TimeZone createGmtTimeZone() {
        return TimeZone.getTimeZone("GMT");
    }

    public static TimeZone createTimeZone(boolean isGmt) {
        return isGmt ? createGmtTimeZone() : TimeZone.getDefault();
    }

    public static String stampToTime(String stamp) {
        String sd = "";
        new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sd = sdf.format(new Date(Long.parseLong(stamp)));
        return sd;
    }

    public static long getDistanceTime(long time1, long time2) {
        return time1 < time2 ? time2 - time1 : time1 - time2;
    }

    public static String getDistanceTimeStr(long time1, long time2) {
        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }

        return getDistanceTimeStr(diff);
    }

    public static String getDistanceTimeStr(long diff) {
        long day = 0L;
        long hour = 0L;
        long min = 0L;
        long sec = 0L;
        day = diff / 86400000L;
        hour = diff / 3600000L - day * 24L;
        min = diff / 60000L - day * 24L * 60L - hour * 60L;
        sec = diff / 1000L - day * 24L * 60L * 60L - hour * 60L * 60L - min * 60L;
        if (day != 0L) {
            return day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
        } else if (hour != 0L) {
            return hour + "小时" + min + "分钟" + sec + "秒";
        } else if (min != 0L) {
            return min + "分钟" + sec + "秒";
        } else {
            return sec != 0L ? sec + "秒" : "0秒";
        }
    }


    @NonNull
    private static SimpleDateFormat getDateFormat() {
        if (sDateFormatThreadLocal == null) {
            Class var0 = DateTimeUtils.class;
            synchronized (DateTimeUtils.class) {
                if (sDateFormatThreadLocal == null) {
                    sDateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
                        protected SimpleDateFormat initialValue() {
                            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                        }
                    };
                }
            }
        }
        return (SimpleDateFormat) sDateFormatThreadLocal.get();
    }


}
